﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace KQ.DCMMP.Common
{
    /// <summary>
    /// 操作Guid公用类
    /// </summary>
    public static class GuidUtility
    {
        #region static

        /// <summary>
        /// 默认空Guid
        /// </summary>
        public static Guid DefaultEmptyGuid = new Guid("00000000-0000-0000-0000-000000000000");

        /// <summary>
        /// 默认空byte数据
        /// </summary>
        public static byte[] DefaultEmptyRAW16 = GuidUtility.ParseHex("00000000000000000000000000000000");

        /// <summary>
        /// 默认空Oracle格式字符串
        /// </summary>
        public static string DefaultEmptyOra = "00000000000000000000000000000000";

        #endregion

        #region Guid Check

        /// <summary>
        /// 判断传来的字符串是否是空Guid
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public static bool IsEmptyGuid(string text)
        {
            string empty = "00000000000000000000000000000000";
            return GuidUtility.DefaultEmptyGuid.Equals(text) || string.Equals(empty, text);
        }

        /// <summary>
        /// 判断传来的字符串是否是Guid
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public static bool IsGuid(string text)
        {
            Regex regex = new Regex("^[A-Fa-f0-9]{8}(-[A-Fa-f0-9]{4}){3}-[A-Fa-f0-9]{12}$",
                RegexOptions.Compiled);
            return regex.IsMatch(text.ToUpper());
        }

        #endregion

        #region Guid

        /// <summary>
        /// 将byte数组转化为Guid
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static Guid ParseGuid(byte[] bytes)
        {
            Guid guid = new Guid(bytes);
            return guid;
        }

        /// <summary>
        /// 将字符串转化为Guid
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public static Guid ParseGuid(string text)
        {
            if (IsGuid(text))
            {
                return Guid.Parse(text);
            }
            else
            {
                byte[] bytes = ParseHex(text);
                Guid guid = new Guid(bytes);
                return guid;
            }
        }

        #endregion

        #region Oracle

        /// <summary>
        /// 将字符串转化为Oracle格式的字符串
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public static string ParseOracle(string text)
        {
            if (text.Contains("-"))
            {
                Guid guid = new Guid(text);
                return BitConverter.ToString(guid.ToByteArray()).Replace("-", "");
            }
            else
            {
                return text;
            }
        }

        /// <summary>
        /// 将Guid转化为Oracle格式的字符串
        /// </summary>
        /// <param name="guid"></param>
        /// <returns></returns>
        public static string ParseOracle(Guid guid)
        {
            return ParseOracle(guid.ToString());
        }

        /// <summary>
        /// 将byte数据转化为Oracle格式字符串
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static string ParseOracle(byte[] bytes)
        {
            StringBuilder sb = new StringBuilder();
            foreach (byte b in bytes)
            {
                sb.Append(b > 15 ? Convert.ToString(b, 16) : '0' + Convert.ToString(b, 16));
            }
            return sb.ToString().ToUpper();
        }

        #endregion

        #region Hex

        /// <summary>
        /// 将Guid转化为byte[]
        /// </summary>
        /// <param name="guid"></param>
        /// <returns></returns>
        public static byte[] ParseHex(Guid guid)
        {
            string ora = GuidUtility.ParseOracle(guid);
            return GuidUtility.ParseHex(ora);
        }

        /// <summary>
        /// 将Oracle格式的Guid转化为byte[]
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public static byte[] ParseHex(string text)
        {
            //判断是否是Guid类型
            string textOra = ParseOracle(text);

            byte[] ret = new byte[textOra.Length / 2];
            for (int i = 0; i < ret.Length; i++)
            {
                ret[i] = Convert.ToByte(textOra.Substring(i * 2, 2), 16);
            }

            return ret;
        }

        #endregion
    }
}
